package com.boarsoft.flow.core;

import java.util.Stack;

/**
 * 维护流程所需线程变量（可选）
 * 
 * @author Mac_J
 *
 */
public class SimpleFlowContext {
	private static ThreadLocal<Stack<SimpleFlow>> threadlocal = new ThreadLocal<>();

	public static SimpleFlow getCurrentFlow() {
		Stack<SimpleFlow> s = threadlocal.get();
		if (s == null) {
			return null;
		}
		if (s.isEmpty()) {
			return null;
		}
		return s.peek();
	}

	public static void setCurrentFlow(SimpleFlow flow) {
		Stack<SimpleFlow> s = threadlocal.get();
		if (s == null) {
			s = new Stack<>();
			threadlocal.set(s);
		}
		s.push(flow);
	}

	public static void removeCurrentFlow() {
		Stack<SimpleFlow> s = threadlocal.get();
		if (s == null) {
			return;
		}
		if (!s.isEmpty()) {
			s.pop();
		}
		if (s.isEmpty()) {
			threadlocal.remove();
		}
	}
}